﻿Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes



Public Enum IndexFormats
    Plain
    HTML
End Enum


Public Enum ThumbnailFormats
    Gif
    Jpeg
    Png
End Enum


Public MustInherit Class Indexable

    <MarshalAs(UnmanagedType.BStr)> Public Const IndexFormat_Plain As String = "text/plain"
    <MarshalAs(UnmanagedType.BStr)> Public Const IndexFormat_HTML As String = "text/html"

    <MarshalAs(UnmanagedType.BStr)> Public Const ThumbnailFormat_Gif As String = "image/gif"
    <MarshalAs(UnmanagedType.BStr)> Public Const ThumbnailFormat_Jpeg As String = "image/jpeg"
    <MarshalAs(UnmanagedType.BStr)> Public Const ThumbnailFormat_Png As String = "image/png"

    Protected Sub New(ByVal content As String, ByVal format As IndexFormats)
        Me._content = content
        Me._format = format
    End Sub


    ''' <summary>
    ''' Indexable content
    ''' </summary>
    ''' <remarks>name="content" description="Indexable content" type="VT_BSTR" required="true" </remarks>
    <MarshalAs(UnmanagedType.BStr)> _
    Private _content As String
    ''' <summary>
    ''' Mime type of the indexable content;  are accepted types
    ''' </summary>
    ''' <remarks> name="format" description="Mime type of the indexable content; text/plain, text/html are accepted types" type="VT_BSTR" required="true"</remarks>
    Private _format As IndexFormats
    ''' <summary>
    ''' The size of the original native content (in bytes)
    ''' </summary>
    ''' <remarks> name="native_size" description="The size of the original native content (in bytes)" type="VT_UI8"</remarks>
    <MarshalAs(UnmanagedType.U8)> _
    Private _native_size As ULong
    ''' <summary>
    ''' Thumbnail image of the content
    ''' </summary>
    ''' <remarks> name="thumbnail" description="Thumbnail image of the content" type="VT_ARRAY"</remarks>
    <MarshalAs(UnmanagedType.ByValArray)> _
    Private _thumbnail As Byte()
    ''' <summary>
    ''' Mime type of the thumbnail; image/gif, image/jpeg, image/png are accepted types
    ''' </summary>
    ''' <remarks>name="thumbnail_format" description="Mime type of the thumbnail; image/gif, image/jpeg, image/png are accepted types" type="VT_BSTR" </remarks>
    Private _thumbnail_format As ThumbnailFormats
    ''' <summary>
    ''' String cookie
    ''' </summary>
    ''' <remarks>name="cookie" description="String cookie" type="VT_BSTR"</remarks>
    <MarshalAs(UnmanagedType.BStr)> _
    Private _cookie As String
    ''' <summary>
    ''' Binary cookie
    ''' </summary>
    ''' <remarks><property name="cookie_raw" description="Binary cookie" type="VT_ARRAY" /></remarks>
    <MarshalAs(UnmanagedType.ByValArray)> _
    Private _cookie_raw As Byte()
    ''' <summary>
    ''' Optional non-indexable string metadata
    ''' </summary>
    ''' <remarks>name="extra_data" description="Optional non-indexable string metadata" type="VT_BSTR" </remarks>
    <MarshalAs(UnmanagedType.BStr)> _
    Private _extra_data As String
    ''' <summary>
    ''' Optional non-indexable binary metadata
    ''' </summary>
    ''' <remarks> name="extra_binary_data" description="Optional non-indexable binary metadata" type="VT_ARRAY" </remarks>
    <MarshalAs(UnmanagedType.ByValArray)> _
    Private _extra_binary_data As Byte()
    ''' <summary>
    ''' Optional additional indexable data
    ''' </summary>
    ''' <remarks>name="other_indexed_data" description="Optional additional indexable data" type="VT_BSTR" </remarks>
    <MarshalAs(UnmanagedType.BStr)> _
    Private _other_indexed_data As String




    Protected _setOtherProperties As Boolean



    ''' <summary>
    ''' Indexable content
    ''' </summary>
    Public ReadOnly Property Content() As String
        Get
            Return _content
        End Get
    End Property
    ''' <summary>
    ''' String cookie
    ''' </summary>
    Public Property Cookie() As String
        Get
            Return _cookie
        End Get
        Set(ByVal value As String)
            _cookie = value

            SetOtherPropertiesFlag()

        End Set
    End Property
    ''' <summary>
    ''' Binary cookie
    ''' </summary>
    Public Property Cookie_raw() As Byte()
        Get
            Return _cookie_raw
        End Get
        Set(ByVal value As Byte())
            _cookie_raw = value

            SetOtherPropertiesFlag()

        End Set
    End Property


    ''' <summary>
    ''' Optional non-indexable binary metadata
    ''' </summary>
    Public Property Extra_binary_data() As Byte()
        Get
            Return _extra_binary_data
        End Get
        Set(ByVal value As Byte())
            _extra_binary_data = value
            SetOtherPropertiesFlag()
        End Set
    End Property
    ''' <summary>
    ''' Optional non-indexable string metadata
    ''' </summary>
    Public Property Extra_data() As String
        Get
            Return _extra_data
        End Get
        Set(ByVal value As String)
            _extra_data = value

            SetOtherPropertiesFlag()
        End Set
    End Property
    ''' <summary>
    ''' Mime type of the indexable content;  are accepted types
    ''' </summary>
    Public ReadOnly Property Format() As IndexFormats
        Get
            Return _format
        End Get
    End Property
    ''' <summary>
    ''' The size of the original native content (in bytes)
    ''' </summary>
    Public Property Native_size() As ULong
        Get
            Return _native_size
        End Get
        Set(ByVal value As ULong)
            _native_size = value
            SetOtherPropertiesFlag()
        End Set
    End Property
    ''' <summary>
    ''' Optional additional indexable data
    ''' </summary>
    Public Property Other_indexed_data() As String
        Get
            Return _other_indexed_data
        End Get
        Set(ByVal value As String)
            _other_indexed_data = value
            SetOtherPropertiesFlag()
        End Set
    End Property
    ''' <summary>
    ''' Thumbnail image of the content
    ''' </summary>
    Public Property Thumbnail() As Byte()
        Get
            Return _thumbnail
        End Get
        Set(ByVal value As Byte())
            _thumbnail = value
            SetOtherPropertiesFlag()
        End Set
    End Property
    ''' <summary>
    ''' Mime type of the thumbnail; image/gif, image/jpeg, image/png are accepted types
    ''' </summary>
    Public Property Thumbnail_format() As ThumbnailFormats
        Get
            Return _thumbnail_format
        End Get
        Set(ByVal value As ThumbnailFormats)
            _thumbnail_format = value
            SetOtherPropertiesFlag()
        End Set
    End Property


    Public Property SetOtherProperties() As Boolean
        Get
            Return _setOtherProperties
        End Get
        Set(ByVal value As Boolean)
            _setOtherProperties = value
        End Set
    End Property
#Region " Create Event "

    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal type As Type) As [Event]
        Return CreateEvent(GetComponentGUID(type))
    End Function
    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    ''' <param name="id">The component's GUID or ProgID.</param>
    Public Function CreateEvent(ByVal id As String) As [Event]

        ' Dim eve As [Event] = GetMyEvent(id)
        Dim eve As New [Event](id, Me.GetType.FullName, EventTypes.Event2)

        AddProperties(eve)

        'AddOtherProperties(eve)

        Return eve

    End Function


    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal type As Type, ByVal eventType As EventTypes) As [Event]
        Return CreateEvent(GetComponentGUID(type), eventType)
    End Function
    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal id As String, ByVal eventType As EventTypes) As [Event]
        Dim eve As New [Event](id, Me.GetType.FullName, eventType)
        AddProperties(eve)
        Return eve
    End Function



    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal type As Type, ByVal eventfactory As Object) As [Event]
        Dim lEventType As EventTypes = EventTypes.Event2
        Return CreateEvent(type, lEventType, eventfactory)
    End Function
    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal type As Type, ByVal eventType As EventTypes, ByVal eventfactory As Object) As [Event]
        Return CreateEvent(GetComponentGUID(type), eventType, eventfactory)
    End Function
    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal id As String, ByVal eventfactory As Object) As [Event]
        Dim lEventType As EventTypes = EventTypes.Event2
        Return CreateEvent(id, lEventType, eventfactory)
    End Function

    ''' <summary>
    ''' CreateEvent
    ''' </summary>
    ''' <returns>Event</returns>
    Public Function CreateEvent(ByVal id As String, ByVal eventType As EventTypes, ByVal eventfactory As Object) As [Event]
        Dim eve As New [Event](id, Me.GetType.FullName, eventType, eventfactory)
        AddProperties(eve)
        Return eve
    End Function


#End Region



    ' Protected MustOverride Function GetMyEvent(ByVal id As String) As [Event]


    Protected Sub SetOtherPropertiesFlag()
        If Not Me.SetOtherProperties Then Me.SetOtherProperties = True
    End Sub
    Private Sub AddProperties(ByVal e As [Event])

        AddRequredProperties(e)

        If Me.SetOtherProperties Then

            AddOtherProperties(e)

        End If

    End Sub
    ''' <summary>
    ''' Add Requred properties
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overridable Sub AddRequredProperties(ByVal e As [Event])

        e.AddProperty("content", Me._content)

        If Me._format = IndexFormats.HTML Then
            e.AddProperty("format", IndexFormat_HTML)
        ElseIf Me._format = IndexFormats.Plain Then
            e.AddProperty("format", IndexFormat_Plain)
        End If
    End Sub
    ''' <summary>
    ''' Add Other Properties
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overridable Sub AddOtherProperties(ByVal e As [Event])

        e.AddProperty("native_size", Me._native_size)

        If Me._thumbnail IsNot Nothing Then
            e.AddProperty("thumbnail", Me._thumbnail)

            Select Case Me._thumbnail_format
                Case ThumbnailFormats.Gif
                    e.AddProperty("thumbnail_format", ThumbnailFormat_Gif)
                Case ThumbnailFormats.Jpeg
                    e.AddProperty("thumbnail_format", ThumbnailFormat_Jpeg)
                Case ThumbnailFormats.Png
                    e.AddProperty("thumbnail_format", ThumbnailFormat_Png)
            End Select


        End If


        If Not String.IsNullOrEmpty(Me._cookie) Then
            e.AddProperty("cookie", Me._cookie)
        End If


        If Me._cookie_raw IsNot Nothing Then
            e.AddProperty("cookie_raw", Me._cookie_raw)
        End If

        If Not String.IsNullOrEmpty(Me._extra_data) Then
            e.AddProperty("extra_data", Me._extra_data)

        End If

        If Me._extra_binary_data IsNot Nothing Then
            e.AddProperty("extra_binary_data", Me._extra_binary_data)
        End If

        If Not String.IsNullOrEmpty(Me._other_indexed_data) Then
            e.AddProperty("other_indexed_data", Me._other_indexed_data)
        End If
    End Sub


    'Public Function GetFullName() As String
    '    Return Me.GetType.FullName
    'End Function

End Class
